		use "$data\4_individual_ano_reg_3.dta", clear
		merge m:1 id wave using  "$data\2_firm_regressions_individual_paper.dta", keepusing(empcat)
		keep if _merge == 3
		
	global outcomes    aminwage wrcon  sosec   empquality_ind lwage
		
	eststo clear

		local c=1 
		foreach o of global outcomes {
			local d=`c'+1
			eststo m`c'`x': areg `o' assignment `o'_2021 $controls_balance missing_`o'_2021 i.wave $se_indiv
			gen inc_in_reg_`o' = e(sample)
		local ++c 
		}
	
	
gen  wage_baseline = lwage_raw_2021 if lwage_raw_2021 != 0

foreach o of global outcomes {
	xtile wage_q_`o' = wage_baseline if inc_in_reg_`o' ==1 , nq(4) 
}


	gen exp2 = experience_sector_base^2
	gen age2 = age_base^2
	
		foreach var of varlist rel man exp experience_sector_base exp2  sup educ_none_base educ_prim_base educ_sec_base educ_tert_base age  {
			egen 		`var'_st = std(`var')
			replace 	`var'_st = 0 if missing(`var')
			gen 		missing_`var' = 0
			replace 	missing_`var' = 1 if missing(`var')
		}
		
		
	

	foreach var in aminwage wrcon sosec empquality_ind  lwage {
	replace `var'_2021 = . if `var'_2021==0
	}
	
	*Run command to create groups  
	foreach var in aminwage wrcon sosec empquality_ind  lwage {
		estrat `var' assignment rel_st missing_rel ///
		man_st missing_man ///
		experience_sector_base_st missing_experience_sector_base ///
		sup_st missing_sup ///
		educ_none_base_st missing_educ_none_base ///
		educ_prim_base_st missing_educ_prim_base ///
		educ_sec_base_st missing_educ_sec_base ///
		educ_tert_base_st missing_educ_tert_base `var'_2021  , reps(10) boot(10) savegroup groups(4) 
		rename estrat_loo_group `var'_group

	}
	
	*Run main regression
	global het_ind group
	global s 3
	global sample "${sample${s}}"

estimates clear 
*aminwage wrcon sosec empquality_ind aminwage wrcon sosec empquality_ind      
local j = 1
foreach h of global het_ind {
    foreach o of varlist aminwage  wrcon sosec empquality_ind lwage {

        local current_outcome = "`o'"
        rename `current_outcome'_group group
		*rename aminwage_group group
		
		ta `h'
		local cats=`r(r)'

     	areg `o' i.assignment##i.`h' `o'_2021 $controls_balance missing_`o'_2021 i.wave $se_indiv  
		eststo m_`o':	margins `h', dydx(1.assignment) post coeflegend
		*gen sample_`o'=e(sample)
	
			* N 
		di "Save N and control group means in macro to include in table" 
		sum `o' if e(sample)
		local ${tbl}_n`j'=	r(N)
			
		* add to estimates 
		estadd 	local 		N_`j' `${tbl}_n`j'':m_`o'
		
		// Now for categories
		forv i=1/`cats' {
		di "sum `o' if `h'==`i' & e(sample)"
			sum `o' if `h'==`i' & e(sample)
			local ${tbl}_n`j'_`i'=r(N)

			sum `o' if assignment==0 & `h'==`i' & e(sample)
			local ${tbl}_cm`j'_`i'=round(r(mean),.001)
			
			* add to estimates 
			estadd 	local 		N_`i'_`j' `${tbl}_n`j'_`i'':m_`o'
			local 	cm_`i' 	= 	string(`${tbl}_cm`j'_`i'', "%9.3f")
			estadd local 		cm_`i'_`j' "`cm_`i''":m_`o'
			
			di "Control group mean: `${tbl}_cm`j'_`i''"
			di "Control group mean: `cm_`i''"
			
			
			}
			
		
		*** Case of 4 categories
		if `cats'==4 {
		di "4 Categories: test for differences in treatment effects"

		** Conduct tests 
		// 2 vs. 1 and 3 vs. 1 are simply the coefficients of the interaction terms
		test _b[1.assignment:2.`h'] =_b[1.assignment:1.`h']
		local ${tbl}_p`j'_12=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 2: `${tbl}_p`j'_12'"
		
		// 3 vs. 1 
		test _b[1.assignment:3.`h'] =_b[1.assignment:1.`h']
		local ${tbl}_p`j'_13=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 3: `${tbl}_p`j'_13'"
		
		// 4 vs. 1 
		test _b[1.assignment:4.`h'] =_b[1.assignment:1.`h']
		local ${tbl}_p`j'_14=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 1 and 4: `${tbl}_p`j'_14'"
		
		// 2 vs. 3 is test for significant difference in coefficients of interaction terms 
		test _b[1.assignment:2.`h'] = _b[1.assignment:3.`h'] 
		local ${tbl}_p`j'_23=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 2 and 3: `${tbl}_p`j'_23'"
		
		// 2 vs. 4
		test _b[1.assignment:2.`h'] = _b[1.assignment:4.`h'] 
		local ${tbl}_p`j'_24=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 2 and 4: `${tbl}_p`j'_24'"
		
		// 3 vs. 4 
		test _b[1.assignment:3.`h'] = _b[1.assignment:4.`h'] 
		local ${tbl}_p`j'_34=round(r(p),.001)
		di "P-val.\ for diff.\ in coeff.\ 3 and 4: `${tbl}_p`j'_34'"
		

		** Add interaction to stored estimates 
		local interaction_12 = string(`${tbl}_p`j'_12', "%9.3f")
		local interaction_13 = string(`${tbl}_p`j'_13', "%9.3f")
		local interaction_14 = string(`${tbl}_p`j'_14', "%9.3f")
		local interaction_23 = string(`${tbl}_p`j'_23', "%9.3f")
		local interaction_24 = string(`${tbl}_p`j'_24', "%9.3f")
		local interaction_34 = string(`${tbl}_p`j'_34', "%9.3f")

		estadd local interaction_12_`j' `interaction_12':m_`o'
		estadd local interaction_13_`j' `interaction_13':m_`o'
		estadd local interaction_14_`j' `interaction_14':m_`o'
		estadd local interaction_23_`j' `interaction_23':m_`o'
		estadd local interaction_24_`j' `interaction_24':m_`o'
		estadd local interaction_34_`j' `interaction_34':m_`o'

		estimates table m*
	}	


        rename group `current_outcome'_group
		*rename group aminwage_group 
		
        local ++j
        local j = `j' - 1

         esttab m* using "$results\01_tables\Table_5_hetero_ind_4groups.tex", ///
            keep(1.assignment:1.`h') ///
            scalars("cm_1_`j' Mean" "N_1_`j' N") ///
            se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace

        esttab m* using "$results\01_tables\Table_5_hetero_ind_4groups.tex", ///
            keep(1.assignment:2.`h') ///
            scalars("cm_2_`j' Mean" "N_2_`j' N") ///
            se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes append
			
		esttab m* using "$results\01_tables\Table_5_hetero_ind_4groups.tex", ///
            keep(1.assignment:3.`h') ///
            scalars("cm_3_`j' Mean" "N_3_`j' N") ///
            se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes append

		esttab m* using "$results\01_tables\Table_5_hetero_ind_4groups.tex", ///
            keep(1.assignment:4.`h') ///
            scalars("cm_4_`j' Mean" "N_4_`j' N" "interaction_12_`j' P-val Diff. between group 1 and 2" "interaction_13_`j' P-val Diff. between group 1 and 3" "interaction_14_`j' P-val Diff. between group 1 and 4" "interaction_23_`j' P-val Diff. between group 2 and 3" "interaction_24_`j' P-val Diff. between group 2 and 4" "interaction_34_`j' P-val Diff. between group 3 and 4") ///
            se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. wage", pattern(1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes append

			
        esttab m*, ///
            keep(1.assignment:1.`h') ///
            scalars("cm_1_`j' Mean" "N_1_`j' N") ///
            se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. wage", pattern(1 1 1 1 1 1)) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace

        esttab m*, ///
            keep(1.assignment:2.`h') ///
            scalars("cm_2_`j' Mean" "N_2_`j' N" ) ///
            se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. wage", pattern(1 1 1 1 1 1)) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace
			
		 esttab m*, ///
            keep(1.assignment:3.`h') ///
            scalars("cm_3_`j' Mean" "N_3_`j' N") ///
            se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. wage", pattern(1 1 1 1 1 1)) ///
            starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace

			esttab m*, ///
            keep(1.assignment:4.`h') ///
            scalars("cm_4_`j' Mean" "N_4_`j' N" "interaction_12_`j' P-val Diff. between group 1 and 2" "interaction_13_`j' P-val Diff. between group 1 and 3" "interaction_14_`j' P-val Diff. between group 1 and 4" "interaction_23_`j' P-val Diff. between group 2 and 3" "interaction_24_`j' P-val Diff. between group 2 and 4" "interaction_34_`j' P-val Diff. between group 3 and 4") ///
            se sfmt(%9.3f) noobs label ///
            mgroups("Min. Wage" "Written Contract" "Social Security" "Formality" "Log. wage", pattern(1 1 1 1 1 1)) ///
			starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers nomtitles nonotes replace
    }
}
